/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.controller;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Maps;
import io.renren.annotation.Login;
import io.renren.annotation.LoginUser;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.ValidatorUtils;
import io.renren.core.dto.LearnHomeBannerDTO;
import io.renren.core.dto.LearnHomeTipsDTO;
import io.renren.core.dto.LearnNewsDTO;
import io.renren.core.dto.LearnUserScoreDTO;
import io.renren.core.service.LearnHomeBannerService;
import io.renren.core.service.LearnHomeTipsService;
import io.renren.core.service.LearnNewsService;
import io.renren.core.service.LearnUserScoreService;
import io.renren.dto.LoginDTO;
import io.renren.entity.UserEntity;
import io.renren.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * banner
 *
 */
@RestController
@RequestMapping("/api/home")
@Api(tags = "测试接口")
public class ApiHomeController {

    @Autowired
    private LearnHomeBannerService learnHomeBannerService;
    @Autowired
    private LearnHomeTipsService learnHomeTipsService;
    @Autowired
    private LearnNewsService learnNewsService;
    @Autowired
    private UserService userService;

    @Autowired
    private LearnUserScoreService learnUserScoreService;

    @GetMapping("info")
    @ApiOperation(value = "获取用户信息")
    public Result<?> userInfo() {
        List<LearnHomeBannerDTO> bannerList = learnHomeBannerService.listAll();
        Map<String, Object> tipParams = Maps.newHashMap();
        tipParams.put("status", 1);
        List<LearnHomeTipsDTO> tipsDTOList = learnHomeTipsService.list(tipParams);
        String tips = "";

        List<LearnNewsDTO> newsList = learnNewsService.listNewsLimit();
        if (CollectionUtils.isNotEmpty(tipsDTOList)) {
            tips = tipsDTOList.get(0).getContent();
        }
        return new Result<>().ok(HomeInfo.builder()
                .bannerList(bannerList)
                .newsList(newsList)
                .tips(tips)
                .build());
    }

    @GetMapping("banner-by-id")
    @ApiOperation(value = "获取banner")
    public Result<?> bannerById(Long id) {
        return new Result<>().ok(learnHomeBannerService.get(id));
    }

    @GetMapping("news-by-id")
    @ApiOperation(value = "获取news")
    public Result<?> newsById(Long id) {
        return new Result<>().ok(learnNewsService.get(id));
    }

    @PostMapping("sign")
    @ApiOperation("签到")
    public Result<LearnUserScoreDTO> sign(HttpServletRequest request) {
        String wxId = request.getHeader("wxId");
        UserEntity user = userService.getByWxId(wxId);
        if (Objects.isNull(user)) {
            return new Result<LearnUserScoreDTO>().error("用户信息错误！");
        }
        LearnUserScoreDTO userScoreDTO = learnUserScoreService.getByUserId(user.getId());
        if (Objects.isNull(userScoreDTO)) {
            userScoreDTO = new LearnUserScoreDTO();
            userScoreDTO.setUserId(user.getId());
            userScoreDTO.setUserPoint(1);
            userScoreDTO.setUserSignDate(new Date());
            userScoreDTO.setUserLearnScore(0);
            userScoreDTO.setCreateDate(new Date());
            userScoreDTO.setUpdateDate(new Date());
            learnUserScoreService.save(userScoreDTO);
        } else {
            if (Objects.nonNull(userScoreDTO.getUserSignDate()) && DateUtil.isSameDay(new Date(), userScoreDTO.getUserSignDate())) {
                return new Result<LearnUserScoreDTO>().error("今日已签到！");
            }
            userScoreDTO.setUserPoint(userScoreDTO.getUserPoint() + 1);
            userScoreDTO.setUserSignDate(new Date());
            userScoreDTO.setUpdateDate(new Date());
            learnUserScoreService.update(userScoreDTO);
        }

        return new Result<LearnUserScoreDTO>().ok(userScoreDTO);
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class HomeInfo {
        private List<LearnHomeBannerDTO> bannerList;
        private List<LearnNewsDTO> newsList;
        private String tips;
    }

}